package com.android.lovely.framework.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.core.view.forEach
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.paging.LoadState
import androidx.paging.PagingDataAdapter
import com.android.lovely.databinding.ViewIssueBinding
import com.android.lovely.databinding.ViewNoContentBinding
import com.android.lovely.databinding.ViewProgressBinding
import com.kotlin.extensions.dimen.Dimension.dp
import com.kotlin.extensions.inflater.LayoutInflaterExt.layoutInflater
import com.kotlin.extensions.view.ViewExt.clickable

/**
 *
 * @author why
 * @date 2025/3/7 23:52
 */

class FlexibleContentContainer(context: Context, attrs: AttributeSet?) :
    FrameLayout(context, attrs) {

    private val noContentBinding by lazy {
        ViewNoContentBinding.inflate(layoutInflater, this, false)
    }

    private val issueBinding by lazy {
        ViewIssueBinding.inflate(layoutInflater, this, false)
    }

    private val progressBinding by lazy {
        ViewProgressBinding.inflate(layoutInflater, this, false)
    }

    init {
        addView(noContentBinding.root)
        addView(issueBinding.root)
        addView(progressBinding.root)
        hideMainContent()
    }

    fun update(){
        val layoutParams = noContentBinding.root.layoutParams
        layoutParams.height = 300.dp
        noContentBinding.root.layoutParams = layoutParams

        val issueLayoutParams = issueBinding.root.layoutParams
        issueLayoutParams.height = 300.dp
        issueBinding.root.layoutParams = layoutParams

        val progressLayoutParams = progressBinding.root.layoutParams
        progressLayoutParams.height = 300.dp
        progressBinding.root.layoutParams = layoutParams

    }

    fun displayIssue() {
        if (issueBinding.root.isVisible) return
        issueBinding.root.showWithAnimation()
        noContentBinding.root.isVisible = false
        progressBinding.root.isVisible = false
        hideMainContent()
    }

    fun displayNoContent() {
        if (noContentBinding.root.isVisible) return
        issueBinding.root.isVisible = false
        noContentBinding.root.showWithAnimation()
        progressBinding.root.isVisible = false
        hideMainContent()
    }

    fun displayProgress() {
        if (progressBinding.root.isVisible) return
        issueBinding.root.isVisible = false
        noContentBinding.root.isVisible = false
        progressBinding.root.showWithAnimation()
        hideMainContent()
    }

    fun displayMainContent() {
        issueBinding.root.isVisible = false
        noContentBinding.root.isVisible = false
        progressBinding.root.isVisible = false
        showMainContent()
    }

    private fun hideMainContent() {
        forEach { child ->
            if (child != issueBinding.root && child != noContentBinding.root && child != progressBinding.root) {
                child.isVisible = false
            }
        }
    }

    private fun showMainContent() {
        forEach { child ->
            if (child != issueBinding.root && child != noContentBinding.root && child != progressBinding.root) {
                child.isVisible = true
            }
        }
    }

    fun setIssueClickListener(listener: () -> Unit) {
        issueBinding.root.clickable {
            listener.invoke()
        }
    }

    fun setNoContentClickListener(listener: () -> Unit) {
        noContentBinding.root.clickable {
            listener.invoke()
        }
    }

    fun setNoContentText(text: String) {
        noContentBinding.message.text = text
    }

    fun setNoContentIcon(id: Int) {
        noContentBinding.icon.setImageResource(id)
    }

    fun setNoContentButton(text: String, show: Boolean = true, onEmptyClick: (() -> Unit)? = null) {
        noContentBinding.button.isVisible = show
        noContentBinding.button.text = text
        noContentBinding.button.clickable {
            onEmptyClick?.invoke()
        }
    }

    fun setLoadState(adapter: PagingDataAdapter<*, *>) {
        adapter.addLoadStateListener { loadState ->
            when (loadState.refresh) {
                is LoadState.NotLoading -> {
                    if (adapter.itemCount == 0) {
                        displayNoContent()
                    } else {
                        displayMainContent()
                    }
                }

                is LoadState.Loading -> {
                    if (adapter.itemCount == 0) {
                        displayProgress()
                    }
                }

                is LoadState.Error -> {
                    if (adapter.itemCount == 0) {
                        displayIssue()
                    }
                }
            }
        }
    }

    private fun View.showWithAnimation() {
        alpha = 0f
        isVisible = true
        animate().alpha(1f).setDuration(300).start()
    }
}
